package com.hams.demo.system.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hams.demo.common.util.ExtAjaxResponse;
import com.hams.demo.common.util.ExtPageable;
import com.hams.demo.system.entity.Department;
import com.hams.demo.system.entity.dto.DepartmentDTO;
import com.hams.demo.system.entity.dto.DepartmentQueryDTO;
import com.hams.demo.system.service.IDepartmentService;

@Controller
@RequestMapping("/department")
public class DepartmentController {
	
	@Autowired
	private IDepartmentService departmentService;

	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Department department) {
		try {
			Department parentDepartment = departmentService.findDepartment(department.getHigherName());
			if(null==parentDepartment) {
				return new ExtAjaxResponse(false, "找不到所填上级部门！");
			}
			if(null == department.getId()) {
				parentDepartment.getChildrenDepartments().add(department);
				department.setParentDepartment(parentDepartment);
				departmentService.saveOrUpdate(parentDepartment);
			} else {
				departmentService.saveOrUpdate(department);
			}
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			System.out.println(e);
			return new ExtAjaxResponse(false, "操作失败！");
		}		
	}
	
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) {
		try {	
			Department department = departmentService.findOne(id);
			if(null!=department) {
				department.setParentDepartment(null);
				departmentService.saveOrUpdate(department);
				departmentService.delete(department);
			}
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			System.out.println(e);
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}
	
	@RequestMapping("/findOne")
	public @ResponseBody Department findOne(@RequestParam Long id) {
		Department department = departmentService.findOne(id);
		return department;
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<DepartmentDTO> findAll() {
		List<Department> lists =  departmentService.findAll();	
		List<DepartmentDTO> departmentDTOs = new ArrayList<DepartmentDTO>();
		for(Department d : lists) {
			DepartmentDTO departmentDTO = new DepartmentDTO();
			BeanUtils.copyProperties(d, departmentDTO);
			departmentDTO.setUsersCount(d.getUser().size());
			departmentDTOs.add(departmentDTO);
		}
		return departmentDTOs;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<Department> findPage(DepartmentQueryDTO departmentQueryDTO, ExtPageable pageable) {
		Page<Department> page = departmentService.findAll(DepartmentQueryDTO.getSpecification(departmentQueryDTO), pageable.getPageable());
		return page;
	}
	
}
